package com.app.newiptv.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.bean.LatelyBean;
import com.app.newiptv.callback.RequestCallback;
import com.app.newiptv.utils.APPLog;
import com.app.newiptv.utils.Paths;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Shinelon on 2017/9/15.
 */

public class LatelyPresenter implements RequestCallback<LatelyBean> {
    private boolean isDisplayed;
    private Callback callback;
    public List<LatelyBean> listContent;
    public List<LatelyBean> collectContent;
    private int position;
    private Context mContext;
    private Gson gson = new Gson();

    public LatelyPresenter(Context mContext) {
        this.mContext = mContext;
    }

    public interface Callback {
        void onError(int code, String msg);

        void onMovieCompleted(LatelyPresenter presenter, int count);
        void onCollectionCompleted(LatelyPresenter presenter, int count);
    }

    public interface MainViewHolder {
        void setlatelyTitle(String title);

        void setlatelyImg(String image);

        void setlateTimer(String timer);
    }
    public interface CollectHolder{
        void setCollectImg(String image);

        void setCollectName(String title);
    }

    @Override
    public void onLoadComplete(String contentID, int pageNumber, int count, List<LatelyBean> contents) {
        if (callback == null || !isDisplayed) {
            APPLog.printInfo("onLoadComplete:callback :");
            return;
        }
        listContent = contents;
        callback.onMovieCompleted(LatelyPresenter.this, count);
    }

    @Override
    public void onLoading(String contentID) {

    }

    @Override
    public void onFailed(String code, String msg) {

    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    //加载数据
    public void loadItem(Bundle params, Callback callback) {
        this.callback = callback;
//        AHservice service = new AHservice();
//        String url = params.getString("url");
//        service.latelyCallback(url, this);
    }
    public void onBindMainChanelView(CollectHolder holder, int position) {
        if (position < collectContent.size()) {
            LatelyBean detailsBean = collectContent.get(position);
            holder.setCollectImg(Paths.HOSTPATH + detailsBean.getImage());
            holder.setCollectName(detailsBean.getName());
        }
    }
    public void latelyPosition(int position) {
        this.position = position;
        if (listContent != null && position < listContent.size()) {
            LatelyBean latelyBean = listContent.get(position);
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("id",latelyBean.getId());
            intent.putExtra("bundle",bundle);
            if(getVodType(position)){
                intent.setClass(mContext, FilmDetailsActivity.class);
                mContext.startActivity(intent);
            }else if(getTelType(position)){
                intent.setClass(mContext, SitcomActivity.class);
                mContext.startActivity(intent);
            }
        }
    }
    public Bundle getParams(int position) {
        Bundle bundle = new Bundle();
        LatelyBean playDetailsBean = listContent.get(position);
        bundle.putInt("id", playDetailsBean.getId());
        return bundle;
    }
    public Bundle getCollectParams(int position) {
        Bundle bundle = new Bundle();
        LatelyBean playDetailsBean = collectContent.get(position);
        bundle.putInt("id", playDetailsBean.getId());
        return bundle;
    }

    public void onBindMainChanelView(MainViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        holder.setlatelyTitle(listContent.get(position).getName());
        holder.setlatelyImg(listContent.get(position).getImage());
        holder.setlateTimer(listContent.get(position).getLateTime());
    }
    public void getCollectCache(){
        SharedPreferences sp = mContext.getSharedPreferences("CACHE_RECORD", Context.MODE_PRIVATE);
        String usedCache = sp.getString("collection_data", "");
        List<LatelyBean> list = gson.fromJson(usedCache, new TypeToken<List<LatelyBean>>() {
        }.getType());
        collectContent = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = list.size() - 1; i >= 0; i--) {
                collectContent.add(list.get(i));
            }
            callback.onCollectionCompleted(LatelyPresenter.this, collectContent.size());
        }
    }


    public void getCacheRecord() {
        SharedPreferences sp = mContext.getSharedPreferences("CACHE_RECORD", Context.MODE_PRIVATE);
        String usedCache = sp.getString("record_data", "");
        List<LatelyBean> list = gson.fromJson(usedCache, new TypeToken<List<LatelyBean>>() {
        }.getType());
        listContent = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = list.size() - 1; i >= 0; i--) {
                listContent.add(list.get(i));
            }
            callback.onMovieCompleted(LatelyPresenter.this, listContent.size());
        }
    }

    public boolean getVodType(int position) {
        LatelyBean playDetailsBean = listContent.get(position);
        if(playDetailsBean.getTotal_episode()==1){
            return true;
        }
        return false;
    }

    public boolean getTelType(int position) {
        LatelyBean playDetailsBean = listContent.get(position);
        if(playDetailsBean.getTotal_episode()>1){
            return true;
        }
        return false;
    }
    public boolean getVodCollectType(int position) {
        LatelyBean playDetailsBean = collectContent.get(position);
        if(playDetailsBean.getTotal_episode()==1){
            return true;
        }
        return false;
    }

    public boolean getTelCollectType(int position) {
        LatelyBean playDetailsBean = collectContent.get(position);
        if(playDetailsBean.getTotal_episode()>1){
            return true;
        }
        return false;
    }
}
